Ext.onReady(function() {

    var store = Ext.create("Ext.data.TreeStore", {
        proxy: {
            type: "ajax",
            url: "/MyCompany/app/get"
        },
        folderSort: true
    });

    var tree = Ext.create("Ext.tree.Panel", {
    	id: "tree",
        title: "view company",
        region: "west",
        width: 500,
        height: 300,
        collapsible: true,
        useArrows: true,
        rootVisible: true,
        store: store,
        multiSelect: false,
        singleExpand: false,
        root: {
            nodeType : "async",
            text     : "My Companies",
            iconCls  : "folder",
            id       : "root",
            expanded : false
        },
        listeners: {
        	render: function() {
        		Ext.getBody().on("contextmenu", Ext.emptyFn, null, {preventDefault: true});
        	},
        	itemcontextmenu: handleOnContextMenuEvent,
        	itemclick: handleOnClickEvent,
        } 
    });
    
    var details = Ext.create("Ext.form.Panel", {
    	id: "details",
    	title: "view details",
    	autoDestroy: false,
    });

    var viewport = Ext.create("Ext.container.Viewport", {
    	layout: "border",
    	items: [tree, details]
    });
    
    Ext.define("My.Company.TextField", {
    	extend: "Ext.form.TextField",
    	alias: "widget.myfield",
    	listeners: {
    		"change": function(sender, newValue, oldValue){
				var result = true;
				if (sender.name == "email"){
					result = Ext.form.VTypes["email"](newValue);
				} else if (sender.name == "name" || sender.name == "customer" || sender.name == "deadline"){
					result = Ext.form.VTypes["alpha"](newValue);
				}
				if (sender.type == "add"){
					result ? Ext.getCmp("buttonPerformAdd").enable() : Ext.getCmp("buttonPerformAdd").disable()
				} else if (sender.type == "edit"){
					result ? Ext.getCmp("submitButton").enable() : Ext.getCmp("submitButton").disable()
				}
						
			}
    	}
    });
    
    Ext.define("My.Company.Panel", {
    	extend: "Ext.form.FormPanel",
    	alias: "widget.mypanel",
    	initComponent: function(){
    		this.items = [];
    		if (this.extraItems){
    			for (var i = 0; i < this.extraItems.length; i++){
    				this.items.push(this.extraItems[i]);
    			}
    		}
    		this.items.push({
    			xtype: "button",
    			id: "submitButton",
    			text: "Save",
    			disabled: true,
    			listeners: {
    				click: submitChanges
    			}
        	});
    	    this.callParent(arguments);
    	}
    });
});